package com.kinglone.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kinglone.model.UserVo;
import com.kinglone.service.UserService;
import com.kinglone.util.ExportXls;
import com.kinglone.util.PoiExcelUtil;
/**
 * 导出excel
 * @author Administrator
 *
 */
@Controller
public class ExportExcelController {
	
	@Autowired
	private UserService userService;

	List<UserVo> userList=null;
	/**
	 * 查询所有的用户信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/showExportExcel.jspx")
	public ModelAndView showExportExcel(HttpServletRequest request,Model model){
		ModelAndView mv = new ModelAndView("UsersExport");
		try {
			List<UserVo> users = userService.getAll();
			if(users != null){
				userList = users;
				model.addAttribute("users", users);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	/**
	 * 导出表
	 * @param request
	 * @param response
	 */
	@RequestMapping("/exportExcel.jspx")
	public void exportExcel(HttpServletRequest request,HttpServletResponse response){
		try {
			ExportXls<UserVo> exportUtils = new ExportXls<UserVo>();
			List<UserVo> list = userList;
			HSSFWorkbook workbook = exportUtils.exportExcel("用户信息表", "", new String[]{"用户名称","用户密码","用户年龄"}, list,0d,0d);
			PoiExcelUtil.write(workbook, response,"用户信息表" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));		
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
}
